<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;

/**
 * 活動分類管理
 *
 * @icon fa fa-circle-o
 */
class ActivityData extends Backend {

	/**
	 * ActivityCategory模型对象
	 * @var \app\admin\model\ActivityCategory
	 */
	protected $model = null;

	public function _initialize() {
		parent::_initialize();
		$this->model = new \app\admin\model\Activity;

	}

	public function import() {
		parent::import();
	}

	/**
	 * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
	 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
	 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
	 */

	public function index() {
		//    var_dump(Session::get('admin'));die;
		$arr = getCenterNameList();

		//当前是否为关联查询
		$this->relationSearch = true;
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('keyField')) {
				return $this->selectpage();
			}
			list($where, $sort, $order, $offset, $limit) = $this->buildparams();

			$obj = $this->model
				->with(['activitycategory'])
				->where($where)
				->where('total_hours', '>', 0);

			$admin = Session::get('admin');
			if ($admin['affiliated_center_id'] != 0) {
				$arr_area = explode(',', $admin['affiliated_center_id']);
				$obj->where(['area_one_id' => ['in', $arr_area]]);
			}
			$list = $obj->order($sort, $order)
				->paginate($limit);
			$domain = request()->domain();
			foreach ($list as $row) {
				$row['area_name'] = '';
				if ($row['area_one_id'] > 0) {
					$row['area_name'] = $arr[$row['area_one_id']];
				}
				$row->getRelation('activitycategory')->visible(['name']);
			}

			$result = array("total" => $list->total(), "rows" => $list->items());

			return json($result);
		}
		$this->assignconfig("activity_edit", $this->auth->check("activity/edit"));
		$this->assignconfig("activity_del", $this->auth->check("activity/del"));
		$this->assignconfig("activity_comment", $this->auth->check("activity_comment/index"));
		return $this->view->fetch();
	}
	/**
	 * 编辑
	 */
	public function edit($ids = null) {

		$adminIds = $this->getDataLimitAdminIds();
		if (is_array($adminIds)) {
			if (!in_array($row[$this->dataLimitField], $adminIds)) {
				$this->error(__('You have no permission'));
			}
		}
		if ($this->request->isAjax()) {

			$params = $this->request->post("row/a");
			if ($params) {
				$params = $this->preExcludeFields($params);
				$result = false;
				Db::startTrans();
				try {
					$activity = Db::name('activity')->where('id', $ids)->find();
					$params['sign_time'] = strtotime($params['sign_time']);
					$params['hours'] = $activity['workinghours'];
					$params['type'] = 1;

					$result = Db::name('activity_sign')->where('activity_id', $ids)
						->where('vo_id', $params['vo_id'])->update($params);
					Db::name('activity')->where('id', $ids)
						->inc('total_hours', $activity['workinghours'])->update();
					Db::name('volunteer')->where('id', $params['vo_id'])
						->inc('workinghours', $activity['workinghours'])->update();
					$vo = Db::name('volunteer')->where('id', $params['vo_id'])->find();

					Db::name('user')->where('id', $vo['user_id'])
						->inc('workinghours', $sign['hours'])->update();
					Db::commit();
				} catch (ValidateException $e) {
					Db::rollback();
					$this->error($e->getMessage());
				} catch (PDOException $e) {
					Db::rollback();
					$this->error($e->getMessage());
				} catch (Exception $e) {
					Db::rollback();
					$this->error($e->getMessage());
				}
				if ($result !== false) {
					$this->success();
				} else {
					$this->error(__('No rows were updated'));
				}
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}

		$this->view->assign("activity_id", $ids);
		return $this->view->fetch();
	}

}
